package com.promise.jokerdream.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.promise.jokerdream.database.dao.*
import com.promise.jokerdream.database.entity.*

/**
 * Room 数据库
 */
@Database(
    entities = [
        UserAccountEntity::class,
        CurrentAccountEntity::class,
        UserConfigEntity::class,
        // 游戏配置实体（27个独立表）
        ConstellationGameConfigEntity::class,
        AbyssGameConfigEntity::class,
        ScratchGameConfigEntity::class,
        MistyGameConfigEntity::class,
        PenguinFightGameConfigEntity::class,
        FightTowerGameConfigEntity::class,
        ExperienceGameConfigEntity::class,
        AscendHeavenGameConfigEntity::class,
        WulinGameConfigEntity::class,
        MercenaryDispatchGameConfigEntity::class,
        ImmortalsGameConfigEntity::class,
        KnightMissionGameConfigEntity::class,
        WorldTreeGameConfigEntity::class,
        FactionCommerceGameConfigEntity::class,
        KnightGameConfigEntity::class,
        ArenaGameConfigEntity::class,
        SectGameConfigEntity::class,
        EscortGameConfigEntity::class,
        FactionFeedDemoGameConfigEntity::class,
        OtherGameConfigEntity::class,
        WlmzGameConfigEntity::class,
        FormationGameConfigEntity::class,
        KnightDreamGameConfigEntity::class,
        CurrencyExchangeGameConfigEntity::class,
        ChaosWarGameConfigEntity::class,
        SectTournamentGameConfigEntity::class,
        KnightArenaGameConfigEntity::class,
        // 任务配置实体
        TaskGroupConfigEntity::class,
        TaskConfigEntity::class,
        TimerTaskConfigEntity::class
    ],
    version = 4,
    exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
    
    abstract fun userAccountDao(): UserAccountDao
    abstract fun currentAccountDao(): CurrentAccountDao
    abstract fun userConfigDao(): UserConfigDao
    // 游戏配置DAO（27个）
    abstract fun constellationGameConfigDao(): ConstellationGameConfigDao
    abstract fun abyssGameConfigDao(): AbyssGameConfigDao
    abstract fun scratchGameConfigDao(): ScratchGameConfigDao
    abstract fun mistyGameConfigDao(): MistyGameConfigDao
    abstract fun penguinFightGameConfigDao(): PenguinFightGameConfigDao
    abstract fun fightTowerGameConfigDao(): FightTowerGameConfigDao
    abstract fun experienceGameConfigDao(): ExperienceGameConfigDao
    abstract fun ascendHeavenGameConfigDao(): AscendHeavenGameConfigDao
    abstract fun wulinGameConfigDao(): WulinGameConfigDao
    abstract fun mercenaryDispatchGameConfigDao(): MercenaryDispatchGameConfigDao
    abstract fun immortalsGameConfigDao(): ImmortalsGameConfigDao
    abstract fun knightMissionGameConfigDao(): KnightMissionGameConfigDao
    abstract fun worldTreeGameConfigDao(): WorldTreeGameConfigDao
    abstract fun factionCommerceGameConfigDao(): FactionCommerceGameConfigDao
    abstract fun knightGameConfigDao(): KnightGameConfigDao
    abstract fun arenaGameConfigDao(): ArenaGameConfigDao
    abstract fun sectGameConfigDao(): SectGameConfigDao
    abstract fun escortGameConfigDao(): EscortGameConfigDao
    abstract fun factionFeedDemoGameConfigDao(): FactionFeedDemoGameConfigDao
    abstract fun otherGameConfigDao(): OtherGameConfigDao
    abstract fun wlmzGameConfigDao(): WlmzGameConfigDao
    abstract fun formationGameConfigDao(): FormationGameConfigDao
    abstract fun knightDreamGameConfigDao(): KnightDreamGameConfigDao
    abstract fun currencyExchangeGameConfigDao(): CurrencyExchangeGameConfigDao
    abstract fun chaosWarGameConfigDao(): ChaosWarGameConfigDao
    abstract fun sectTournamentGameConfigDao(): SectTournamentGameConfigDao
    abstract fun knightArenaGameConfigDao(): KnightArenaGameConfigDao
    // 任务配置DAO
    abstract fun taskGroupConfigDao(): TaskGroupConfigDao
    abstract fun taskConfigDao(): TaskConfigDao
    abstract fun timerTaskConfigDao(): TimerTaskConfigDao
    
    companion object {
        private const val DATABASE_NAME = "joker_dream_database"
        
        @Volatile
        private var INSTANCE: AppDatabase? = null
        
        /**
         * 数据库迁移：从版本 3 到版本 4
         * 添加 createdAt 字段到 user_accounts 表
         */
        private val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 添加 createdAt 字段，默认值为当前时间戳（毫秒）
                database.execSQL(
                    "ALTER TABLE user_accounts ADD COLUMN createdAt INTEGER NOT NULL DEFAULT 0"
                )
            }
        }
        
        fun getInstance(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    DATABASE_NAME
                )
                    .addMigrations(MIGRATION_3_4)
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}

